BIN ?= net_interface_test

# CROSS_PREFIX    ?= arm-fullhanv3-linux-uclibcgnueabi-


CC           = $(CROSS_PREFIX)gcc
CXX = $(CROSS_PREFIX)g++

AS           = $(CROSS_PREFIX)as
AR           = $(CROSS_PREFIX)ar
LD           = $(CROSS_PREFIX)ld
RM           = rm -rf
MKDIR        = mkdir
OBJDUMP      = $(CROSS_PREFIX)objdump
OBJCOPY	     = $(CROSS_PREFIX)objcopy
STRIP	     = $(CROSS_PREFIX)strip
SIZE         = $(CROSS_PREFIX)size


Q=@
E=echo
ifeq ($(V), 1)
Q=
E=true
endif
ifeq ($(QUIET), 1)
Q=@
E=true
endif


#WARNINGS = -Wall -Wextra \
           -Wshadow -Wundef -Wmaybe-uninitialized -Wmissing-prototypes -Wpointer-arith -Wuninitialized \
           -Wunreachable-code -Wreturn-type -Wmultichar -Wdouble-promotion -Wclobbered -Wdeprecated  \
           -Wempty-body -Wshift-negative-value \
           -Wtype-limits -Wsizeof-pointer-memaccess
#-Werror
WARNINGS =  -Wextra \
           -Wshadow -Wundef -Wmaybe-uninitialized -Wmissing-prototypes -Wpointer-arith -Wuninitialized \
           -Wunreachable-code -Wreturn-type -Wmultichar -Wdouble-promotion -Wclobbered -Wdeprecated  \
           -Wempty-body -Wstack-usage=2048 \
           -Wtype-limits -Wsizeof-pointer-memaccess -Wno-unused-value -Wno-unused-parameter
CPP_WARNINGS = -Werror -Wextra \
           -Wshadow -Wundef -Wmaybe-uninitialized  -Wpointer-arith -Wuninitialized \
           -Wunreachable-code -Wreturn-type -Wmultichar -Wdouble-promotion -Wclobbered -Wdeprecated  \
           -Wempty-body -Wstack-usage=2048 \
           -Wtype-limits -Wsizeof-pointer-memaccess -Wno-unused-value -Wno-unused-parameter		   
#-Wshift-negative-value 
OPTIMIZATION ?= -O3 -g0
INC_DIR = ./ ../
SRCDIR = ./

#Locations
BUILDPATH  ?= build


CFLAGS += $(patsubst %, -I%, $(INC_DIR)) 
DEFINES =__LINUX_OS__ _GNU_SOURCE 


CFLAGS +=-std=c11 $(patsubst %, -D%, $(DEFINES))  $(WARNINGS) $(OPTIMIZATION) 
CPPFLAGS += -std=c++11 $(patsubst %, -I%, $(INC_DIR))  $(patsubst %, -D%, $(DEFINES))  $(CPP_WARNINGS) $(OPTIMIZATION) 



LIB_DIR = 
LIB = m pthread rt
LDFLAGS ?= $(patsubst %, -L%, $(LIB_DIR))  $(patsubst %, -l%, $(LIB))   -Wl,-Map=$(BIN).map -Wl,-gc-sections
#LDFLAGS ?=  -lm -linput -lpng



CSRCS += $(foreach d, $(SRCDIR), $(wildcard $d/*.c))
CPPSRCS += $(foreach d, $(SRCDIR), $(wildcard $d/*.cpp))

COBJS += $(patsubst %.c, $(BUILDPATH)/%.o, $(CSRCS))
CPPOBJS += $(patsubst %.cpp,$(BUILDPATH)/%.o, $(CPPSRCS))

OBJEXT ?= .o

# AOBJS = $(ASRCS:.S=$(OBJEXT))
# COBJS = $(CSRCS:.c=$(OBJEXT))

SRCS = $(ASRCS) $(CSRCS) 
OBJS = $(AOBJS) $(COBJS) $(CPPOBJS)

## MAINOBJ -> OBJFILES
.PHONY: all  clean mkdir debug prepare default
all: prepare default 

prepare:
ifneq ($(BUILDPATH),)
	@for i in $(SRCDIR); \
	do mkdir -p $(BUILDPATH)/$$i; \
	done
endif



$(BUILDPATH)/%.o: %.c
	$(Q)$(CC) $(CFLAGS) -c $< -o $@
	@$(E) "CC $<"
$(BUILDPATH)/%.o: %.cpp
	$(Q)$(CXX) $(CPPFLAGS) -c $< -o $@
	@$(E) "CC $<"

default: $(BIN)

$(BIN):$(OBJS)
	$(CXX) -o $(BIN)   $(OBJS)$(LDFLAGS)

clean:
	rm -rf $(BUILDPATH) $(BIN) $(OBJS)  $(BIN).map *.txt

debug:
	@echo "OBJS:$(OBJS)"
	@echo "CSRCS:$(CSRCS)"
	@echo "COBJS:$(COBJS)"
	@echo "CPPOBJS:$(CPPOBJS)"
